const app = getApp();
const util = require('../../utils/util.js');
Page({
    data: {
        dataList: [],
        show_fly_img: false,
        goodsImageFly: '',
        title: '本地有',
        loading: false,
        color: '#FFFFFF',
        background: '#D50012',
        animated: false
    },
    onLoad: function (options) {
        this.setData({
            title: options.title
        })
        util.share();
        var self = this;
        var special_id = options.special_id;
        self.setData({
            special_id: special_id,
        })
        wx.getStorage({
            key: 'key',
            success: (res) => {
                self.setData({
                    key: res.data,
                })
            }
        })
        wx.getStorage({
            key: 'special_' + special_id,
            success: (res) => {
                if (Date.parse(new Date()) - res.data.cachTime > app.globalData.cachTempTime) {
                    self.getRequest(special_id);
                } else {
                    self.show_datas(res.data);
                }
            },
            fail: () => {
                //获取列表数据
                self.getRequest(special_id);
            }
        })


        this.cartPos = {};
        this.cartPos['x'] = app.globalData.sw - 99;
        this.cartPos['y'] = app.globalData.sh - 45;
    },
    getRequest: function (special_id) {
        var self = this

        wx.request({
            url: app.globalData.ApiUrl + '/index.php?act=index&op=wechat_special',
            data: {
                special_id: special_id
            },
            success(res) {

                var data = res.data.datas;
                data.cachTime = Date.parse(new Date());
                wx.setStorage({
                    key: 'special_' + special_id,
                    data: data
                })

                self.show_datas(data);
            },
            complete: function () {
                wx.hideNavigationBarLoading() //完成停止加载
                wx.stopPullDownRefresh() //停止下拉刷新

            }
        })

    },
    show_datas: function (data_list) {
        var self = this
        var data = data_list.list
        for (var index in data) {
            switch (data[index].key) {
                case 'adv_list':
                case 'home3':
                case 'goods_class':
                    for (var i in data[index].value.item) {
                        data[index].value.item[i].url = util.buildUrl(data[index].value.item[i].type, data[index].value.item[i].data);
                    }

                    break;
                case "broad":
                    for (var i in data[index].value.list) {
                        data[index].value.list[i].url = util.buildUrl(data[index].value.list[i].type, data[index].value.list[i].data);
                    }

                    break;
                case 'home1':
                    data[index].value.url = util.buildUrl(data[index].value.type, data[index].value.data);
                    break;

                case 'home2':
                case 'home4':
                    data[index].value.square_url = util.buildUrl(data[index].value.square_type, data[index].value.square_data);
                    data[index].value.rectangle1_url = util.buildUrl(data[index].value.rectangle1_type, data[index].value.rectangle1_data);
                    data[index].value.rectangle2_url = util.buildUrl(data[index].value.rectangle2_type, data[index].value.rectangle2_data);
                    break;
                case 'home5':
                    data[index].value.url = util.buildUrl(data[index].value.type, data[index].value.data);
                    break;
                case 'home6':
                    data[index].value.rectangle1_url = util.buildUrl(data[index].value.rectangle1_type, data[index].value.rectangle1_data);
                    data[index].value.rectangle2_url = util.buildUrl(data[index].value.rectangle2_type, data[index].value.rectangle2_data);
                    data[index].value.rectangle3_url = util.buildUrl(data[index].value.rectangle3_type, data[index].value.rectangle3_data);
                    data[index].value.rectangle4_url = util.buildUrl(data[index].value.rectangle4_type, data[index].value.rectangle4_data);
                    data[index].value.rectangle5_url = util.buildUrl(data[index].value.rectangle5_type, data[index].value.rectangle5_data);
                    break;
                case 'home7':
                    data[index].value.rectangle1_url = util.buildUrl(data[index].value.rectangle1_type, data[index].value.rectangle1_data);
                    data[index].value.rectangle2_url = util.buildUrl(data[index].value.rectangle2_type, data[index].value.rectangle2_data);
                    data[index].value.rectangle3_url = util.buildUrl(data[index].value.rectangle3_type, data[index].value.rectangle3_data);
                    data[index].value.rectangle4_url = util.buildUrl(data[index].value.rectangle4_type, data[index].value.rectangle4_data);
                    break;
                case 'home8':
                    data[index].value.rectangle1_url = util.buildUrl(data[index].value.rectangle1_type, data[index].value.rectangle1_data);
                    data[index].value.rectangle2_url = util.buildUrl(data[index].value.rectangle2_type, data[index].value.rectangle2_data);
                    break;
                case 'home9':
                    data[index].value.rectangle1_url = util.buildUrl(data[index].value.rectangle1_type, data[index].value.rectangle1_data);
                    data[index].value.rectangle2_url = util.buildUrl(data[index].value.rectangle2_type, data[index].value.rectangle2_data);
                    data[index].value.rectangle3_url = util.buildUrl(data[index].value.rectangle3_type, data[index].value.rectangle3_data);
                    data[index].value.rectangle4_url = util.buildUrl(data[index].value.rectangle4_type, data[index].value.rectangle4_data);
                    break;
                case 'home10':
                    data[index].value.rectangle1_url = util.buildUrl(data[index].value.rectangle1_type, data[index].value.rectangle1_data);
                    data[index].value.rectangle2_url = util.buildUrl(data[index].value.rectangle2_type, data[index].value.rectangle2_data);
                    data[index].value.rectangle3_url = util.buildUrl(data[index].value.rectangle3_type, data[index].value.rectangle3_data);
                    data[index].value.rectangle4_url = util.buildUrl(data[index].value.rectangle4_type, data[index].value.rectangle4_data);
                    break;
                case 'home11':
                    data[index].value.rectangle1_url = util.buildUrl(data[index].value.rectangle1_type, data[index].value.rectangle1_data);
                    data[index].value.rectangle2_url = util.buildUrl(data[index].value.rectangle2_type, data[index].value.rectangle2_data);
                    data[index].value.rectangle3_url = util.buildUrl(data[index].value.rectangle3_type, data[index].value.rectangle3_data);
                    data[index].value.rectangle4_url = util.buildUrl(data[index].value.rectangle4_type, data[index].value.rectangle4_data);
                    data[index].value.rectangle5_url = util.buildUrl(data[index].value.rectangle5_type, data[index].value.rectangle5_data);
                    data[index].value.rectangle6_url = util.buildUrl(data[index].value.rectangle6_type, data[index].value.rectangle6_data);
                    break;
                case "goods":
                    for (var i in data[index].value.item) {
                        data[index].value.item[i].url = '/pages/goods_detail/goods_detail?goods_id=' + data[index].value.item[i].goods_id;
                    }

                    break;
                case 'video_list':
            }
        }
        self.setData({
            dataList: data
        })
    },

    onShow: function () {
        var self = this
        wx.getStorage({
            key: 'address',
            success: function (res) {
                self.setData({
                    address: res.data
                })
            },
        });
        util.tabCalCartNum();
    },
    gotoCart: function () {
        wx.switchTab({
            url: '../cart/cart'
        })
    },
    addCart: function (e) {
        let goods_id = e.currentTarget.dataset['goods_id'];
        let goods_img = e.currentTarget.dataset['goods_img'];
        let quantity = 1;
        this.setData({
            goodsImageFly: goods_img
        })

        wx.getStorage({
            key: 'key',
            success: (res) => {
                wx.request({
                    url: app.globalData.ApiUrl + '/index.php?act=member_cart&op=wechat_cart_add',
                    data: {
                        key: res.data,
                        goods_id: goods_id,
                        quantity: quantity
                    },
                    method: 'POST',
                    header: {
                        'content-type': 'application/x-www-form-urlencoded'
                    },
                    dataType: 'json',
                    success: (res) => {
                        if (res.data.code == 200) {
                            wx.getStorage({
                                key: 'cart_count',
                                success: (res) => {
                                    wx.setTabBarBadge({
                                        index: 3,
                                        text: String(res.data + quantity)
                                    })
                                    this.setData({
                                        cart_count: this.data.cart_count + 1
                                    })
                                    this.addcartAnimation(e);
                                },
                            })
                        } else {
                            wx.showToast({
                                title: res.data.datas.error,
                                icon: 'none'
                            })
                        }
                    }
                });
            },
            fail: () => {
                if (app.globalData.authorized) {
                    wx.navigateTo({
                        url: "../chooselogin/chooselogin"
                    })
                } else {
                    wx.navigateTo({
                        url: "../login/login"
                    })
                }
            }
        })
    },
    preventTouchMove: function () {
        return false;
    },
    addcartAnimation: function (e) {
        if (this.data.show_fly_img)
            return;
        this.finger = {};
        var topPoint = {};
        this.finger['x'] = e.touches["0"].clientX - 70;
        this.finger['y'] = e.touches["0"].clientY - 145;
        if (this.finger['x'] - this.cartPos['x'] < 0) {
            topPoint['x'] = (this.cartPos['x'] - this.finger['x']) / 2 + this.finger['x'];
        } else {
            topPoint['x'] = Math.abs(this.cartPos['x'] - this.finger['x']) / 2 + this.cartPos['x'];
        }
        if (this.finger['y'] < this.cartPos['y']) {
            topPoint['y'] = this.finger['y'] - 80;
        } else {
            topPoint['y'] = this.cartPos['y'] - 80;
        }
        this.linePos = app.bezier([this.finger, topPoint, this.cartPos], 30);
        this.startAnimation();
    },
    startAnimation: function () {
        var index = 0,
            self = this,
            bezier_points = this.linePos['bezier_points'];
        this.setData({
            show_fly_img: true,
            fly_x: self.finger['x'],
            fly_y: self.finger['y']
        })
        this.timer = setInterval(() => {
            self.setData({
                fly_x: bezier_points[index]['x'],
                fly_y: bezier_points[index]['y']
            })
            index++;
            if (index >= 30) {
                clearInterval(self.timer);
                self.setData({
                    show_fly_img: false
                })
            }
        }, 22);
    },
    onPullDownRefresh: function () {
        wx.showToast({
            title: '加载中',
            icon: 'loading',
            duration: 400
        });
        wx.showNavigationBarLoading() //在标题栏中显示加载
        this.getRequest(this.data.special_id);

    },
    onShareAppMessage: function () {
        let share_key = this.data.key ? this.data.key : '';
        let share_path = '';
        if (util.getCurrentPageUrlWithArgs().indexOf("?") != -1) {
            share_path = util.getCurrentPageUrlWithArgs() + '&share_key=' + share_key;
        } else {
            share_path = util.getCurrentPageUrlWithArgs() + '?share_key=' + share_key;
        }
        return {
            title: this.data.data.special_desc,
            imageUrl: this.data.dataList[0].adv_list.item[0].image,
            path: share_path
        }
    }
})